package org.openrbac.rbac.controller;

import org.openrbac.rbac.controller.base.BaseController;
import org.openrbac.rbac.entity.Image;
import org.openrbac.rbac.entity.Product;
import org.openrbac.rbac.entity.User;
import org.openrbac.rbac.service.CatalogueService;
import org.openrbac.rbac.service.ImageService;
import org.openrbac.rbac.service.ProductService;
import org.openrbac.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 梁富贵
 * &#064;Date  2024/5/2
 **/
@RestController
@RequestMapping("/public")
public class publicController extends BaseController<UserService, User> {
    @Autowired
    private ImageService imageService;
    @Autowired
    private ProductService productService;

    @Autowired
    private CatalogueService catalogueService;
    @GetMapping("/getCaptcha")
    public void editPassword(HttpServletResponse response) throws IOException {
        BufferedImage img = service.getCaptcha();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 将BufferedImage写入到ByteArrayOutputStream中
        ImageIO.write(img, "png", baos);
        // 将ByteArrayOutputStream转换为字节数组
        byte[] captchaImageBytes = baos.toByteArray();

        response.setContentType("image/png");
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);

        try (ServletOutputStream outputStream = response.getOutputStream()) {
            outputStream.write(captchaImageBytes);
            outputStream.flush();
        }
    }

    @PostMapping("/checkCaptcha")
    public Boolean checkCaptcha(@RequestBody String captcha) {
        if (service.checkCaptcha(captcha.replace("\"",""))) {
            return true;
        }else {
            return false;
        }
    }

    @GetMapping("/image/getPicture")
    public List<Image> getPicture(@RequestParam("key") String type){
        List<Image> images =  imageService.getImages(type);
        if(images.isEmpty()){
            return null;
        }
        return images;
    }

    @GetMapping("/getRecommandProducts")
    public List<Map<String,Object>> getRecommandProducts(){
            List<Integer> pids = productService.getRecommandIds("normal");
            List<Integer> iids = productService.getRecommandIds("normal");

            List<Image> list1 = imageService.getRecommandGoods(iids);

            List<Product> list2 = productService.getGoods(pids);

            List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list2.size(); i++) {
            Map<String,Object> hashMap = new HashMap<>();
            hashMap.put("url",(list1.get(i).getUrl()+"/"+list1.get(i).getName()));
            hashMap.put("id",list2.get(i).getId());
            hashMap.put("price",list2.get(i).getPrice());
            hashMap.put("name",list2.get(i).getName());
            result.add(i,hashMap);
        }
        return result;
    }

}
